﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Logic.Web;
using LotteryVote.Entity;
using LotteryVote.Logic;
using LotteryVote.Logic.Exceptions;
using LotteryVote.Utility;
using System.Text.RegularExpressions;

namespace LotteryVote
{
    public partial class AddUser : AgentBasePage
    {
        UserFacade userFacade = new UserFacade();
        protected void Page_Load(object sender, EventArgs e)
        {
            BindPackage();
        }
        void BindPackage()
        {
            Execute(() =>
            {
                if (CurrentUser.Role != SystemRole.Agent) return;
                if (!IsPostBack)
                {
                    CommisionGroupFacade blCommGroup = new CommisionGroupFacade();
                    ddl_package.DataSource = blCommGroup.GetCommissionGroupBySpecie(LotterySpeciesEnum.VietnamLottery);
                    ddl_package.DataTextField = CommissionGroup.GROUPNAME;
                    ddl_package.DataValueField = CommissionGroup.GROUPID;
                    ddl_package.DataBind();
                }
            });
        }
        public string GetAgentType()
        {
            SystemRole role = CurrentUser.Role + 1;
            return GetGlobalResourceObject("Resource", role.ToString()).ToString();
        }

        protected void btn_submit_Click(object sender, EventArgs e)
        {
            Execute(() =>
            {
                NewUser();
                ShowMessage("Result", "Add Success!");
            });
        }

        void NewUser()
        {
            var user = BuildUser();
            int[] species = new int[1];
            if (ck_lotterySpecie.Checked)
                species[0] = (int)LotterySpeciesEnum.VietnamLottery;
            switch (CurrentUser.Role)
            {
                case SystemRole.Company: userFacade.AddSuper(user, species); break;
                case SystemRole.Super: userFacade.AddMaster(user, species); break;
                case SystemRole.Master: userFacade.AddAgent(user, species); break;
                case SystemRole.Agent: AddMember(user, species); break;
                default: throw new NoPermissionException("AddUser", "UserId:" + CurrentUser.UserId);
            }
        }
        void AddMember(User user, int[] species)
        {
            int group = int.Parse(ddl_package.SelectedValue);
            userFacade.AddGuest(user, new[] { group }, species);
        }

        User BuildUser()
        {
            ValidFormItem();
            string userName = txt_userName.Text.Trim();
            string name = txt_name.Text.Trim();
            string myPassword = txt_myPassword.Text;
            string password = txt_password.Text;
            string confirmPwd = txt_comfirmPwd.Text;
            decimal givenCredit;
            decimal.TryParse(txt_givenCredit.Text, out givenCredit);
            string email = txt_Email.Text;
            double shareRate;
            double.TryParse(txt_shareRate.Text, out shareRate);
            return UserFacade.BuildNewUser(CurrentUser.UserId, userName, password, name, givenCredit, shareRate, lv_userState.SelectedState, email);
        }

        void ValidFormItem()
        {
            if (!EncryptHelper.Equal(txt_myPassword.Text, CurrentUser.UserInfo.Password))
                throw new PromptException("Password Error!");
            if(txt_password.Text!=txt_comfirmPwd.Text)
                throw new PromptException("Password Not Equal!");
            if (!(txt_givenCredit.Text.IsFloat() || txt_shareRate.Text.IsFloat()))
                throw new PromptException("Not avalidale num!");
        }
    }
}